package com.ctsi.ssdc.service.impl;

import com.ctsi.ssdc.entity.Excel12;
import com.ctsi.ssdc.entity.FixedInvestment;
import com.ctsi.ssdc.mapper.Excel12Mapper;
import com.ctsi.ssdc.service.Excel12Service;
import com.ctsi.ssdc.utils.TimeUtil;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Future;

/**
 * 9基础表工业分行业(效益)(Excel12)表服务实现类
 *
 * @author makejava
 * @since 2023-02-20 13:06:26
 */
@Service("excel12Service")
public class Excel12ServiceImpl implements Excel12Service {
    @Resource
    private Excel12Mapper excel12Dao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Excel12 queryById(Long id) {
        return this.excel12Dao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param excel12 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Excel12> queryByPage(Excel12 excel12, PageRequest pageRequest) {
        long total = this.excel12Dao.count(excel12);
        return new PageImpl<>(this.excel12Dao.queryAllByLimit(excel12, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param excel12 实例对象
     * @return 实例对象
     */
    @Override
    public Excel12 insert(Excel12 excel12) {
        this.excel12Dao.insert(excel12);
        return excel12;
    }

    /**
     * 修改数据
     *
     * @param excel12 实例对象
     * @return 实例对象
     */
    @Override
    public Excel12 update(Excel12 excel12) {
        this.excel12Dao.update(excel12);
        return this.queryById(excel12.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.excel12Dao.deleteById(id) > 0;
    }

    /**
     * 批量增加
     * @param list
     * @return
     */
    @Override
    public boolean insertBatch(List<Excel12> list) {
        return excel12Dao.insertBatch(list)>0;
    }

    /**
     * 房地产投资当月 + 累计 + 累计同比
     * @param year
     * @param month
     * @return
     */
    @Override
    public FixedInvestment getRealEstateInvestmentInfo(String year, String month) {
        FixedInvestment info = excel12Dao.getRealEstateInvestmentInfo(year, month);
        if (info == null) return null;
        FixedInvestment lastYear = excel12Dao.getRealEstateInvestmentInfo(String.valueOf(Integer.parseInt(year) - 1), month);
        if (lastYear != null) {
            double mom = Math.round((info.getAccumulate() / lastYear.getAccumulate() - 1) * 1000) / 10;
            info.setMonthOnMonth(mom);
        }
        if (info != null) {
            info.setStatisticalTime(TimeUtil.date(year,month));
            info.setIndicators("#房地产投资");
        }
        return info;
    }


    /**
     * 根据日期删除
     * @param date
     * @return 是否成功
     */
    @Override
    public boolean removeByDate(Date date) {
        return excel12Dao.removeByDate(date);
    }

}
